package com.bytz.modules.cms.product.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bytz.common.api.vo.Result;
import com.bytz.modules.cms.common.util.ExcelUtil;
import com.bytz.modules.cms.product.entity.ProductPack;
import com.bytz.modules.cms.product.model.MallProductPackBatchImportExcel;
import com.bytz.modules.cms.product.model.ProductPackModel;
import com.bytz.modules.cms.product.service.IMallProductPackService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@Api(tags = "产品打包分类")
@RequestMapping("/pack")
public class MallPackController {

    @Autowired
    private IMallProductPackService packService;

    @ApiOperation("通过包名称右模糊匹配")
    @RequestMapping(value = "/search", method = RequestMethod.GET)
    public Result<List<ProductPackModel>> productPackByCode(@RequestParam String code) {

        return Result.ok(packService.searchPack(code));
    }

    @ApiOperation("通过包id查询商品详情")
    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    public Result<ProductPackModel> packageDetail(@RequestParam String id) {
        return Result.ok(packService.getPackDetail(id));
    }

    @ApiOperation(value = "分页查询", notes = "分页查询")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Result<IPage<ProductPackModel>> page(ProductPackModel packModel,
                                                @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                HttpServletRequest req) {
        return Result.ok(packService.queryPage(new Page<>(pageNo, pageSize), packModel, req.getParameterMap()));
    }

    @ApiOperation(value = "删除", notes = "删除")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public Result<Boolean> delete(@RequestParam String id) {
        return Result.ok(packService.deleteById(id));
    }

    @ApiOperation(value = "点击编辑按钮进行回显", notes = "点击编辑按钮进行回显")
    @RequestMapping(value = "/updateProductPack", method = RequestMethod.POST)
    public Result<ProductPackModel> updateProductPack(@RequestBody ProductPackModel productPackModel) {
        ProductPackModel result = packService.updateProductPack(productPackModel);
        return Result.ok(result);
    }

    @ApiOperation(value = "新建打包", notes = "新建打包")
    @RequestMapping(value = "/createPack", method = RequestMethod.POST)
    public Result<ProductPack> createPack(@RequestBody ProductPack productPack) {
        packService.createPack(productPack);
        return Result.ok(productPack);
    }

    @ApiOperation(value = "在已建好的打包里添加关联商品，或保存当前编辑",notes = "在已建好的打包里添加关联商品，或保存当前编辑")
    @RequestMapping(value = "/createPackDetails",method = RequestMethod.POST)
    public Result<Boolean> createPackDetails(@RequestBody ProductPackModel productPackModel) {
        packService.createPackDetails(productPackModel);
        return Result.ok();
    }

    @ApiOperation(value = "获取导入模板", notes = "获取导入模板")
    @RequestMapping(value = "/template", method = RequestMethod.POST)
    public void template(HttpServletResponse response) throws Exception {
        ExcelUtil.exportTemp("批量导入到商品包", "Sheet1", MallProductPackBatchImportExcel.class, response);
    }

    @ApiOperation(value = "通过excel进行批量添加", notes = "通过excel进行批量添加")
    @RequestMapping(value = "/getImportProduct", method = RequestMethod.POST)
    public Result<ProductPackModel> importExcel(HttpServletRequest request) {
        //这个ids是商品编码
        List<String> productNumbers = ExcelUtil.importGroup(request, MallProductPackBatchImportExcel.class, null)
                .stream().map(MallProductPackBatchImportExcel::getProductNumber)
                .collect(Collectors.toList());
        ProductPackModel result = packService.importProductPackDetail(productNumbers);
        return Result.ok(result);
    }


}
